package com.link.base.base.buryingpoint.buriedanalysis.service;

import javax.annotation.Resource;

import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.link.base.base.buryingpoint.buriedanalysis.dao.mybatis.mapper.BuriedAnalysisReportMapper;
import com.link.base.base.buryingpoint.buriedanalysis.model.BuriedAnalysisReport;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 埋点数据分析报表-埋点数据分析报表
 *
 * @author 闫有霞
 * @date 2019/05/06 15:52
 */
@Service
public class BuriedAnalysisReportServiceImpl extends BasicServiceImpl<BuriedAnalysisReport> implements BuriedAnalysisReportService {

    @Resource
    private BuriedAnalysisReportMapper buriedAnalysisReportMapper;

    @Override
    public BasicMapper<BuriedAnalysisReport> getBasicMapper() {
        return buriedAnalysisReportMapper;
    }

    @Override
    public List<BuriedAnalysisReport> queryByAppId(BuriedAnalysisReport entity) throws Exception {
        //先初始化,7天日期，对应值默认为零
        List<BuriedAnalysisReport> list = new ArrayList<>();
        if (StringUtils.equals("CREATED", entity.getGroupByField())) {
            list = formatReportList();
            //查询数据库统计的结果
            List<BuriedAnalysisReport> queryList = buriedAnalysisReportMapper.queryByAppId(entity);
            //两者对比，把没有查到的日期对应的值默认为零，必须保证有七组值
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < queryList.size(); j++) {
                    if (list.get(i).getCreated().equals(queryList.get(j).getCreated())) {
                        list.get(i).setPageViewsNum(queryList.get(j).getPageViewsNum());
                        list.get(i).setVisitorsNum(queryList.get(j).getVisitorsNum());
                        list.get(i).setVisitsNum(queryList.get(j).getVisitsNum());
                        break;
                    }
                }
            }
        } else {
            list = buriedAnalysisReportMapper.queryByAppId(entity);
        }
        return list;
    }

    @Override
    public List<BuriedAnalysisReport> queryShareByAppId(BuriedAnalysisReport entity) throws Exception {
        List<BuriedAnalysisReport> shareList = new ArrayList<>();
        // 获取总的访问次数
        BuriedAnalysisReport allParam = new BuriedAnalysisReport();
        allParam.setAppId(entity.getAppId());
        List<BuriedAnalysisReport> allList = buriedAnalysisReportMapper.queryByAppId(allParam);
        BuriedAnalysisReport allAnalysis = allList.get(0);
        //总访问次数
        Double allVisitsNum = allAnalysis.getVisitsNum();
        // 获取每个用户的转发分享次数
        shareList = buriedAnalysisReportMapper.queryShareByAppId(entity);
        //计算分享比
        if (allVisitsNum != 0.0) {
            for (BuriedAnalysisReport share : shareList) {
                // 转发分享次数
                Double forwardShareNum = share.getForwardShareNum();
                Double shareRate = forwardShareNum / allVisitsNum;
                share.setShareRate(shareRate);
            }
        }
        return shareList;
    }

    /**
     * 根据广告ID查询当日浏览量数据
     *
     * @author: 闫有霞
     * @date: 2019/5/6
     * @param: appId 广告ID
     **/
    @Override
    public List<BuriedAnalysisReport> queryTodayByAppId(BuriedAnalysisReport entity) throws Exception {
        //先初始化,1天24小时，对应值默认为零
        List<BuriedAnalysisReport> list = new ArrayList<>();
        list = formatTodayList();
        //查询数据库统计的结果
        List<BuriedAnalysisReport> queryList = buriedAnalysisReportMapper.queryTodayByAppId(entity);
        //两者对比，把没有查到的日期对应的值默认为零，必须保证有24组值
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < queryList.size(); j++) {
                if (list.get(i).getCreated().equals(queryList.get(j).getCreated())) {
                    list.get(i).setPageViewsNum(queryList.get(j).getPageViewsNum());
                    list.get(i).setVisitorsNum(queryList.get(j).getVisitorsNum());
                    list.get(i).setVisitsNum(queryList.get(j).getVisitsNum());
                    break;
                }

            }
        }
        for (BuriedAnalysisReport report : list) {
            String created = formartData(report.getCreated());
            report.setCreated(created);
        }
        return list;

    }


    /**
     * 初始化list（统计前七天数据）
     *
     * @return
     */
    private List<BuriedAnalysisReport> formatReportList() {
        List<BuriedAnalysisReport> list = new ArrayList<>();
        for (int k = 0; k < 7; k++) {
            BuriedAnalysisReport tmp = new BuriedAnalysisReport();
            String tmpDate = getDate(k);
            tmp.setCreated(tmpDate);
            tmp.setPageViewsNum(0.00);
            tmp.setVisitorsNum(0.00);
            tmp.setVisitsNum(0.00);
            list.add(tmp);
        }
        return list;
    }

    /**
     * 初始化list（统计一天24小时数据）
     *
     * @return
     */
    private List<BuriedAnalysisReport> formatTodayList() {
        List<BuriedAnalysisReport> list = new ArrayList<>();
        for (int k = 0; k < 24; k++) {
            BuriedAnalysisReport tmp = new BuriedAnalysisReport();
            String tmpDate = getTodayTime(k);
            tmp.setCreated(tmpDate);
            tmp.setPageViewsNum(0.00);
            tmp.setVisitorsNum(0.00);
            tmp.setVisitsNum(0.00);
            list.add(tmp);
        }
        return list;
    }

    /**
     * 用于获取日期
     *
     * @param i
     * @return
     */
    private String getDate(int i) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -(6 - i));
        String result = f.format(ca.getTime());
        return result;
    }

    /**
     * 用于获取1天24小时
     *
     * @param i
     * @return
     */
    private String getTodayTime(int i) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        Date date = new Date();
        ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH), ca.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date beginOfDate = ca.getTime();
        ca.setTime(beginOfDate); //设置时间为当前时间
        ca.add(Calendar.HOUR_OF_DAY, i);
        String result = f.format(ca.getTime());
        return result;
    }

    /**
     * 格式化日期  2019-05-13 21
     * 格式化为   21
     *
     * @author: 闫有霞
     * @date: 2019/5/13
     * @param:
     **/
    private String formartData(String value) {
        return value.substring(10, value.length());
    }
}

